import { defineStore } from 'pinia'
import { listWeeklyMenu, addWeeklyMenu, updateWeeklyMenu, delWeeklyMenu } from '@/api/catering/weeklyMenu'
import { listCareMenu, addCareMenu, updateCareMenu, delCareMenu } from '@/api/catering/careMenu'
import { listFeedback, addFeedback, responseFeedback, delFeedback } from '@/api/catering/feedback'

// 每周食谱状态管理
export const useWeeklyMenuStore = defineStore('weeklyMenu', {
  state: () => ({
    weeklyMenuList: [],
    weeklyMenuLoading: false,
    weeklyMenuTotal: 0,
    weeklyMenuParams: {
      pageNum: 1,
      pageSize: 10,
      menuDate: '',
      mealType: null,
      status: null
    }
  }),
  
  actions: {
    // 获取每周食谱列表
    async getWeeklyMenuList(params = {}) {
      this.weeklyMenuLoading = true
      try {
        const response = await listWeeklyMenu({ ...this.weeklyMenuParams, ...params })
        this.weeklyMenuList = response.records || []
        this.weeklyMenuTotal = response.total || 0
        return response
      } catch (error) {
        console.error('获取每周食谱列表失败:', error)
        throw error
      } finally {
        this.weeklyMenuLoading = false
      }
    },

    // 新增每周食谱
    async addWeeklyMenu(data) {
      try {
        const response = await addWeeklyMenu(data)
        // 刷新列表
        await this.getWeeklyMenuList()
        return response
      } catch (error) {
        console.error('新增每周食谱失败:', error)
        throw error
      }
    },

    // 更新每周食谱
    async updateWeeklyMenu(data) {
      try {
        const response = await updateWeeklyMenu(data)
        // 刷新列表
        await this.getWeeklyMenuList()
        return response
      } catch (error) {
        console.error('更新每周食谱失败:', error)
        throw error
      }
    },

    // 删除每周食谱
    async deleteWeeklyMenu(id) {
      try {
        const response = await delWeeklyMenu(id)
        // 刷新列表
        await this.getWeeklyMenuList()
        return response
      } catch (error) {
        console.error('删除每周食谱失败:', error)
        throw error
      }
    },

    // 更新查询参数
    updateParams(params) {
      this.weeklyMenuParams = { ...this.weeklyMenuParams, ...params }
    }
  }
})

// 关怀食谱状态管理
export const useCareMenuStore = defineStore('careMenu', {
  state: () => ({
    careMenuList: [],
    careMenuLoading: false,
    careMenuTotal: 0,
    careMenuParams: {
      pageNum: 1,
      pageSize: 10,
      menuType: null,
      status: null
    }
  }),
  
  actions: {
    // 获取关怀食谱列表
    async getCareMenuList(params = {}) {
      this.careMenuLoading = true
      try {
        const response = await listCareMenu({ ...this.careMenuParams, ...params })
        this.careMenuList = response.records || []
        this.careMenuTotal = response.total || 0
        return response
      } catch (error) {
        console.error('获取关怀食谱列表失败:', error)
        throw error
      } finally {
        this.careMenuLoading = false
      }
    },

    // 新增关怀食谱
    async addCareMenu(data) {
      try {
        const response = await addCareMenu(data)
        // 刷新列表
        await this.getCareMenuList()
        return response
      } catch (error) {
        console.error('新增关怀食谱失败:', error)
        throw error
      }
    },

    // 更新关怀食谱
    async updateCareMenu(data) {
      try {
        const response = await updateCareMenu(data)
        // 刷新列表
        await this.getCareMenuList()
        return response
      } catch (error) {
        console.error('更新关怀食谱失败:', error)
        throw error
      }
    },

    // 删除关怀食谱
    async deleteCareMenu(id) {
      try {
        const response = await delCareMenu(id)
        // 刷新列表
        await this.getCareMenuList()
        return response
      } catch (error) {
        console.error('删除关怀食谱失败:', error)
        throw error
      }
    },

    // 更新查询参数
    updateParams(params) {
      this.careMenuParams = { ...this.careMenuParams, ...params }
    }
  }
})

// 用餐反馈状态管理
export const useFeedbackStore = defineStore('feedback', {
  state: () => ({
    feedbackList: [],
    feedbackLoading: false,
    feedbackTotal: 0,
    feedbackParams: {
      pageNum: 1,
      pageSize: 10,
      menuDate: '',
      mealType: null,
      elderlyId: null,
      status: null
    }
  }),
  
  actions: {
    // 获取用餐反馈列表
    async getFeedbackList(params = {}) {
      this.feedbackLoading = true
      try {
        const response = await listFeedback({ ...this.feedbackParams, ...params })
        this.feedbackList = response.records || []
        this.feedbackTotal = response.total || 0
        return response
      } catch (error) {
        console.error('获取用餐反馈列表失败:', error)
        throw error
      } finally {
        this.feedbackLoading = false
      }
    },

    // 新增用餐反馈
    async addFeedback(data) {
      try {
        const response = await addFeedback(data)
        // 刷新列表
        await this.getFeedbackList()
        return response
      } catch (error) {
        console.error('新增用餐反馈失败:', error)
        throw error
      }
    },

    // 回复用餐反馈
    async responseFeedback(data) {
      try {
        const response = await responseFeedback(data)
        // 刷新列表
        await this.getFeedbackList()
        return response
      } catch (error) {
        console.error('回复用餐反馈失败:', error)
        throw error
      }
    },

    // 删除用餐反馈
    async deleteFeedback(id) {
      try {
        const response = await delFeedback(id)
        // 刷新列表
        await this.getFeedbackList()
        return response
      } catch (error) {
        console.error('删除用餐反馈失败:', error)
        throw error
      }
    },

    // 更新查询参数
    updateParams(params) {
      this.feedbackParams = { ...this.feedbackParams, ...params }
    }
  }
}) 